#!/usr/bin/env python
# -*- coding: utf-8 -*-
#  cron_job.py
#  scheduler list

#  Created by zhouzongli633 on 2020/7/10 9:52 上午.
#  Copyright ©Intellectus All rights reserved.
import datetime
import threading

from Common.logger import app_log
from apscheduler.schedulers.background import BackgroundScheduler
from Schedule.update_discharged_date import update_discharged_date
from Schedule.member_signup_worker import member_signup_worker
from Schedule.dump_contents import dump_mysql


class ScheduleManager(object):
    _instance_lock = threading.Lock()

    def __new__(cls, *args, **kwargs):
        if not hasattr(ScheduleManager, "_instance"):
            with ScheduleManager._instance_lock:
                if not hasattr(ScheduleManager, "_instance"):
                    ScheduleManager._instance = object.__new__(cls)
        app_log.info("ScheduleManager init...")
        return ScheduleManager._instance

    def __init__(self):
        self.scheduler = BackgroundScheduler()
        self.scheduler.start()

    def add_schedule_test(self):
        self.scheduler.add_job(update_discharged_date, trigger="cron", second='*/2')

    def member_signup_worker(self, user_id):
        run_date = datetime.datetime.now().replace(microsecond=0) + datetime.timedelta(minutes=5)
        self.scheduler.add_job(member_signup_worker, args=user_id, trigger="date", run_date=run_date)
        self.scheduler.remove_job()
    def add_backup_educations(self):
        self.scheduler.add_job(dump_mysql, trigger="cron", day_of_week='wed', hour='3', minute='30')
        # self.scheduler.add_job(dump_mysql, trigger="cron", minute='35')

    def init_tasks(self):
        # 自动备份数据库
        self.add_backup_educations()


if __name__ == "__main__":
    manager = ScheduleManager()
